package com.demo.jsf;
import java.io.Serializable;
import java.util.ArrayList;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class QuizBean implements Serializable {
   private ArrayList<ProblemBean> problems = new ArrayList<ProblemBean>();     
   private int currentIndex;  
   private int score;
   public QuizBean() {      
      problems.add(
         new ProblemBean("制造日期与有效日期是同一天的产品是什么？" ,"报纸" )); 
      problems.add(
         new ProblemBean("什么东西肥得快，瘦得更快 ？" ,"气球" ));
      problems.add(
        new ProblemBean("放一支铅笔在地上,要使任何人都无法跨过,怎么做?" ,"放在墙边" ));
      problems.add(
         new ProblemBean("青蛙为什么能跳得比树高？" ,"树不会跳" ));
      problems.add(
        new ProblemBean("最不听话的是谁？" ,"聋子" ));
   }   

   public void setProblems(ArrayList<ProblemBean> newValue) { 
      problems = newValue;
   } 
      
   public int getScore() { return score; }

   public ProblemBean getCurrent() {
       return problems.get(currentIndex);
   }

   public String getAnswer() { return ""; }
   public void setAnswer(String newValue) { 
      try 
      {
         if (getCurrent().getAnswer().equals(newValue))score+=20;            
             currentIndex=((currentIndex+1)% problems.size());
      }catch (Exception ex) {
          System.out.printf(ex.toString());
      }
   }   

}
